package com.czkt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.SalChance;
import com.czkt.pojo.SysUser;
import com.czkt.pojo.vo.SysUserVo;
import com.czkt.service.ISalChanceService;
import com.czkt.service.ISysUserService;
import com.czkt.utils.PageUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/chance")
public class SalChanceController {

    @Autowired
    ISalChanceService salChanceService;

    @Autowired
    PageUtils pageUtils;

    @RequestMapping("/list")
    public Object list(SalChance salChance, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "2") Integer pageSize, Model model) {
        IPage<SalChance> page = new Page<>(pageIndex, pageSize);

        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        System.out.println(salChance.toString());

        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.hasText(salChance.getChcCustName()), SalChance::getChcCustName, salChance.getChcCustName())
                .like(StringUtils.hasText(salChance.getChcDesc()), SalChance::getChcDesc, salChance.getChcDesc())
                .eq(currentUser.getRole().getRoleId() == 2,SalChance::getChcDueId,currentUser.getUsrId());

        page = salChanceService.page(page, queryWrapper);

        BeanUtils.copyProperties(page, pageUtils);

        model.addAttribute("chancePage", pageUtils);
        model.addAttribute("chcCustName", salChance.getChcCustName());
        model.addAttribute("chcDesc", salChance.getChcDesc());

        return "chance/list";
    }

    @Autowired
    ISysUserService sysUserService;

    @RequestMapping("/add")
    public Object add(Model model) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getUsrRoleId,2).or().eq(SysUser::getUsrRoleId,10);
        model.addAttribute("users",sysUserService.list(queryWrapper));
        return "chance/add";
    }

    @RequestMapping("/save")
    public Object save(SalChance salChance){
        if (salChanceService.saveOrUpdate(salChance)){
            return "redirect:chance/list";
        } else if (salChance.getChcId() != null){
            return "chance/edit";
        } else {
            return "redirect:chance/add";
        }
    }

    @RequestMapping("/edit")
    public Object edit(Long chcId, Model model) {
        model.addAttribute("chance", salChanceService.getById(chcId));
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getUsrRoleId,2).or().eq(SysUser::getUsrRoleId,10);
        model.addAttribute("users",sysUserService.list(queryWrapper));
        return "chance/edit";
    }

    @RequestMapping("/del/{chcId}")
    @ResponseBody
    public Object del(@PathVariable("chcId") long chcId){
        Map<String,String> map = new HashMap<>();

        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SalChance::getChcId,chcId).isNotNull(SalChance::getChcStatus);

        if (salChanceService.remove(queryWrapper)){
            map.put("delResult","true");
        } else {
            map.put("delResult","false");
        }
        return map;
    }
}